package com.apestech.formwork.xml.core.exec;

import com.apestech.formwork.xml.core.NodeBase;
import com.apestech.formwork.xml.core.var.NodeVarMapping;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.run.datamapper.MappingData;
import com.apestech.framework.run.datamapper.SingleDataMappingModule;
import com.apestech.framework.xml.pub.core.ElementUtil;
import org.dom4j.Element;

import java.util.ArrayList;

/**
 * 属于MAPPING类的结点 </br>
 * 该类主要用途将XML结点数据，对应入、出参类型的结点，转义为未加工的ArrayList<SingleDataMappingModule>类型输出 </br>
 * 生成输出结果，可在ExecData里完成与数据关联输出的JSONObject </br>
 *
 * @author hup
 * @create 2018-04-24-14:15
 */
public class FieldMapping extends ElementUtil {

    private Element node;
    private String name;

    public FieldMapping(Element node){
        this.node=node;
        this.name=node.getName();
    }

    /**
     * 获取MAPPING对应数据
     * @param in
     * @return
     * @throws Exception
     */
    public JSONObject getMapping(JSONObject in) throws Exception {
        return getJson(getExecParam(), in);
    }

    private JSONObject getJson(ArrayList<SingleDataMappingModule> param, JSONObject in) throws Exception {
        MappingData data=new MappingData();
        data.setParams(param);
        return data.getData(in);
    }

    /**
     * 获取单个执行参数
     * @return
     * @throws Exception
     */
    public ArrayList<SingleDataMappingModule> getExecParam() throws Exception {
        if(node==null) return null;
        NodeBase ni=new NodeBase(node);
        ni.getNodeInfo();
        ArrayList<Element> nodes=ni.getNodes();
        int len=nodes.size();
        ArrayList<SingleDataMappingModule> list=new ArrayList<SingleDataMappingModule>();
        for(int i=0; i<len; i++){
            Element e=nodes.get(i);
            list.add(getField(e));
        }
        return list;
    }

    /**
     * 获取单个字段执行的数据
     * @param n
     * @return
     * @throws Exception
     */
    private SingleDataMappingModule getField(Element n) throws Exception {
        SingleDataMappingModule out=new SingleDataMappingModule();
        if(n==null){
            return out;
        }
        NodeBase ni=new NodeBase(n);
        ni.getNodeInfo();
        ArrayList<Element> nodes=ni.getNodes();
        JSONObject nAttr=ni.getAttr();
        String express=null;
        String mathId=null;
        int size=0;
        if(nodes!=null){
            int len=nodes.size();
            for(int i=0; i<len; i++){
                Element e=nodes.get(i);
                String name=e.getName();
                NodeBase ei=new NodeBase(e);
                ei.getNodeInfo();
                JSONObject eAttr=ei.getAttr();
                if(ei.getText()!=null){
                    express=ei.getText();
                } else if(eAttr!=null){
                    mathId=eAttr.getString("id");
                    size=castToInt(eAttr.getString("size"));
                }
            }
        }
        out.setField(nAttr.getString(NodeVarMapping.getFieldId()),nAttr.getString(NodeVarMapping.getTARGET()),nAttr.getString(NodeVarMapping.getFieldName()),
                nAttr.getString(NodeVarMapping.getFieldType()), nAttr.getString(NodeVarMapping.getAllowNull()), nAttr.getString(NodeVarMapping.getDefaultValue()),
                nAttr.getString(NodeVarMapping.getFieldCheck()),nAttr.getString(NodeVarMapping.getVALUE()), nAttr.getString(NodeVarMapping.getTableName()),
                express, mathId, size);
        return out;
    }

}
